package com.family.web.controller.pedigree;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.family.common.core.controller.BaseController;
import com.family.common.core.domain.AjaxResult;
import com.family.common.core.page.TableDataInfo;
import com.family.pedigree.domain.FamilyUserIncome;
import com.family.pedigree.service.IFamilyUserIncomeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 个人收入表 前端控制器
 * </p>
 *
 * @author forever
 * @since 2025-01-16
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/family/user/income")
public class FamilyUserIncomeController extends BaseController {

    private final IFamilyUserIncomeService familyUserIncomeService;

    @PostMapping
    public AjaxResult add(@Validated @RequestBody FamilyUserIncome familyUserIncome){
        familyUserIncomeService.save(familyUserIncome);
        return AjaxResult.success();
    }

    @PutMapping
    public AjaxResult edit(@Validated @RequestBody FamilyUserIncome familyUserIncome){
        familyUserIncomeService.updateById(familyUserIncome);
        return AjaxResult.success();
    }

    @DeleteMapping("/{incomeId}")
    public AjaxResult delById(@PathVariable Long incomeId){
        familyUserIncomeService.removeById(incomeId);
        return AjaxResult.success();
    }

    @GetMapping("/page")
    public TableDataInfo page(FamilyUserIncome familyUserIncome){
        startPage();
        List<FamilyUserIncome> result = familyUserIncomeService.list(
                Wrappers.<FamilyUserIncome>lambdaQuery()
                        .eq(Objects.nonNull(familyUserIncome.getInfoId()), FamilyUserIncome::getInfoId, familyUserIncome.getInfoId())
                        .gt(Objects.nonNull(familyUserIncome.getAnnualIncome()), FamilyUserIncome::getAnnualIncome, familyUserIncome.getAnnualIncome())
        );
        return getDataTable(result);
    }
}
